import warnings
warnings.filterwarnings('ignore')
# importing libraries
import cv2
import numpy as np
import matplotlib.pyplot as plt
from scipy import signal
# importing images
sar1 = cv2.imread(r'C:\Users\sejit\Downloads\Oil-Spill-Classification-main\images\SAR#1.png', 0)
sar2 = cv2.imread(r'C:\Users\sejit\Downloads\Oil-Spill-Classification-main\images\SAR#2.png', 0)
sar3 = cv2.imread(r'C:\Users\sejit\Downloads\Oil-Spill-Classification-main\images\SAR#3.png', 0)
def plotHistogram(original, img, name):
count, value = np.histogram(img, 255, [0, 255])
plt.figure(figsize = (16, 6))
plt.subplot(1, 2, 1)
plt.imshow(img, cmap="gray")
plt.title(name)
plt.subplot(1, 2, 2)
plt.stem(count)
plt.legend(["Gray shade count"])
plt.xlabel("Pixel Density")
plt.ylabel("Pixel Frequency")
plt.title("Histogram")
img = sar1.copy()
imgCopy = sar1.copy()
# image preprocessing first
plotHistogram(img, img, "Before any preprocessing")
plt.show()
# image stats first
r, c = img.shape
print("Image Resolution:", r, "x", c)
# apply median filter to remove noise from the image's background
plt.figure(figsize=(25, 25))
final = cv2.medianBlur(img, 3)
plotHistogram(img, final, "After preprocessing")
plt.show()
img = final.copy()
plt.figure(figsize=(20, 20))
SE = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
# thresholding with the T from above histogram
for i in range(r):
for j in range(c):
img[i, j] = 0 if img[i, j] > 40 else 255
Performing Opening Morphological Operation to get rid of any noise dots in the background
plt.figure(figsize=(20, 20))
# erording image
erodedImage = cv2.erode(img, SE)
# then dilating image
openImage = cv2.dilate(erodedImage, SE)
plt.subplot(131)
plt.imshow(img, cmap="gray")
plt.title("Pre-processed image")
plt.axis("off")
plt.subplot(132)
plt.imshow(openImage, cmap="gray")
plt.title("Opened Image")
plt.axis("off")
Identifying the edges of the oil spill to identify the actual spread rather than the region
#Detect horizontal edges using in built convolution function
mask = [
[-1, -2, -1],
[0, 0, 0],
[1, 2, 1],
]
outputHorizontal = signal.convolve2d(openImage, mask)
# showing the output image after convolution
plt.figure(figsize=(7, 7))
plt.imshow(outputHorizontal, cmap="gray", vmin=0, vmax=255)
plt.title("Horizontal edge detection")
#Detect vertical edges using inbuilt function for convolution
mask = [
[-1, 0, 1],
[-2, 0, 2],
[-1, 0, 1],
]
outputVertical = signal.convolve2d(openImage, mask)
# showing the output image after convolution
plt.figure(figsize=(7, 7))
plt.imshow(outputVertical, cmap="gray", vmin=0, vmax=255)
plt.title("Vertical edge detection")
# Detecting vertical and horizontal edges
G = np.absolute(outputVertical) + np.absolute(outputHorizontal)
# showing the output image after adding them
plt.figure(figsize=(15, 15))
plt.subplot(121)
plt.imshow(G, cmap="gray", vmin=0, vmax=255)
plt.title("Combined edges detection")
plt.axis("off")
# final comparison of the original and the gotten images
plt.figure(figsize=(18, 18))
plt.subplot(131)
plt.imshow(imgCopy, cmap="gray")
plt.axis("off")
plt.title("Original Image")
plt.subplot(132)
plt.imshow(openImage, cmap="gray")
plt.axis("off")
plt.title("Opened Image")
plt.subplot(133)
plt.imshow(G, cmap="gray")
plt.axis("off")
plt.title("Detected Oil Spill Edges")
img = sar2.copy()
imgCopy = sar2.copy()
# image preprocessing first
plotHistogram(img, img, "Before any preprocessing")
plt.show()
# image stats first
r, c = img.shape
print("Image Resolution:", r, "x", c)
# apply median filter to remove noise from the image
plt.figure(figsize=(25, 25))
final = cv2.medianBlur(img, 3)
plotHistogram(img, final, "After preprocessing")
plt.show()
img = final.copy()
Selecting the proper threshold as the histogram shown above as 70
plt.figure(figsize=(20, 20))
SE = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
# thresholding
for i in range(r):
for j in range(c):
img[i, j] = 0 if img[i, j] > 70 else 255
Performing Opening Morphological Operation to get rid of any noise dots in the background
plt.figure(figsize=(20, 20))
# erording image
erodedImage = cv2.erode(img, SE)
# then dilating image
openImage = cv2.dilate(erodedImage, SE)
plt.subplot(131)
plt.imshow(img, cmap="gray")
plt.title("Pre-processed image")
plt.axis("off")
plt.subplot(132)
plt.imshow(openImage, cmap="gray")
plt.title("Opened Image")
plt.axis("off")
Identifying the edges of the oil spill to identify the actual spread rather than the region
#Detect horizontal edges using in built convolution function
mask = [
[-1, -2, -1],
[0, 0, 0],
[1, 2, 1],
]
outputHorizontal = signal.convolve2d(openImage, mask)
# showing the output image after convolution
plt.figure(figsize=(7, 7))
plt.imshow(outputHorizontal, cmap="gray", vmin=0, vmax=255)
plt.title("Horizontal edge detection")
#Detect vertical edges using inbuilt function for convolution
mask = [
[-1, 0, 1],
[-2, 0, 2],
[-1, 0, 1],
]
outputVertical = signal.convolve2d(openImage, mask)
# showing the output image after convolution
plt.figure(figsize=(7, 7))
plt.imshow(outputVertical, cmap="gray", vmin=0, vmax=255)
plt.title("Vertical edge detection")
# Detecting vertical and horizontal edges
G = np.absolute(outputVertical) + np.absolute(outputHorizontal)
# showing the output image after adding them
plt.figure(figsize=(15, 15))
plt.subplot(121)
plt.imshow(G, cmap="gray", vmin=0, vmax=255)
plt.title("Combined edges detection")
plt.axis("off")
# final comparison of the original and the gotten images
plt.figure(figsize=(18, 18))
plt.subplot(131)
plt.imshow(imgCopy, cmap="gray")
plt.axis("off")
plt.title("Original Image")
plt.subplot(132)
plt.imshow(openImage, cmap="gray")
plt.axis("off")
plt.title("Opened Image")
plt.subplot(133)
plt.imshow(G, cmap="gray")
plt.axis("off")
plt.title("Detected Oil Spill Edges")
img = sar3.copy()
imgCopy = sar3.copy()
# image preprocessing first
plotHistogram(img, img, "Before any preprocessing")
plt.show()
# image stats first
r, c = img.shape
print("Image Resolution:", r, "x", c)
Solution: Histogram equalization
count, value = np.histogram(img, 255, [0, 255])
cdf = count.cumsum() / count.sum()
eq = (cdf * 255).astype(int)
for i in range(r):
for j in range(c):
img[i, j] = eq[img[i, j]]
plt.figure(figsize = (15, 10))
plt.subplot(1, 2, 1)
plt.imshow(imgCopy, cmap="gray")
plt.title("Original image")
plt.axis("off")
plt.subplot(1, 2, 2)
plt.imshow(img, cmap="gray")
plt.axis("off")
plt.title("Equalized image")
plt.show()
# plotting the new histogram of the equalized image
plotHistogram(img, img, "After preprocessing")
plt.show()
As you can see, our image has too many details visible. Since we don't have so much details in our image we'll try to reduce the background noise.
# apply median filter to remove noise from the image
plt.figure(figsize=(25, 25))
final = cv2.blur(img, (5, 5))
plotHistogram(img, final, "After preprocessing")
plt.show()
img = final.copy()
# apply median filter to remove noise from the image
plt.figure(figsize=(25, 25))
final = cv2.medianBlur(img, 5)
plotHistogram(img, final, "After preprocessing")
plt.show()
Selecting threshold as 40
plt.figure(figsize=(20, 20))
SE = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
# thresholding
for i in range(r):
for j in range(c):
img[i, j] = 0 if img[i, j] > 40 else 255
plt.figure(figsize=(20, 20))
# erording image
erodedImage = cv2.erode(img, SE)
# then dilating image
openImage = cv2.dilate(erodedImage, SE)
plt.subplot(131)
plt.imshow(img, cmap="gray")
plt.title("Pre-processed image")
plt.axis("off")
plt.subplot(132)
plt.imshow(openImage, cmap="gray")
plt.title("Opened Image")
plt.axis("off")
Detecting the edges and the boundary of the affected region due to Oil Spill
#Detect horizontal edges using in built convolution function
mask = [
[-1, -2, -1],
[0, 0, 0],
[1, 2, 1],
]
outputHorizontal = signal.convolve2d(openImage, mask)
# showing the output image after convolution
plt.figure(figsize=(7, 7))
plt.imshow(outputHorizontal, cmap="gray", vmin=0, vmax=255)
plt.title("Horizontal edge detection")
#Detect vertical edges using inbuilt function for convolution
mask = [
[-1, 0, 1],
[-2, 0, 2],
[-1, 0, 1],
]
outputVertical = signal.convolve2d(openImage, mask)
# showing the output image after convolution
plt.figure(figsize=(7, 7))
plt.imshow(outputVertical, cmap="gray", vmin=0, vmax=255)
plt.title("Vertical edge detection")
# Detecting vertical and horizontal edges
G = np.absolute(outputVertical) + np.absolute(outputHorizontal)
# showing the output image after adding them
plt.figure(figsize=(15, 15))
plt.subplot(121)
plt.imshow(G, cmap="gray", vmin=0, vmax=255)
plt.title("Combined edges detection")
plt.axis("off")
# final comparison of the original and the gotten images
plt.figure(figsize=(18, 18))
plt.subplot(131)
plt.imshow(imgCopy, cmap="gray")
plt.axis("off")
plt.title("Original Image")
plt.subplot(132)
plt.imshow(openImage, cmap="gray")
plt.axis("off")
plt.title("Opened Image")
plt.subplot(133)
plt.imshow(G, cmap="gray")
plt.axis("off")
plt.title("Detected Oil Spill Edges")